// 定义获取登录用户api
import {getLoginState, setLoginState} from "../config/globalState";
import request from "../request/request";
import {showFailToast} from "vant";

// 登录
const doLogin = async (userAccount: string, userPassword: string) => {
    const result = await request.post('/user/login', {
        userAccount: userAccount,
        userPassword: userPassword
    })
    if (result.data.code === 200) {
        let loginUser = result.data.data;
        setLoginState(loginUser);
        return loginUser;
    }
}

// 获取当前登录用户
const getCurrentUser = async () => {
    const loginUser = getLoginState();
    if (loginUser) {
        return loginUser;
    } else {
        // 请求后端当前登录用户
        const res = await request.get('/user/current');
        if (res.data?.data) {
            return res.data?.data;
        }
    }
}

// 获取推荐用户列表
const getRecommendUsers = async (isMatchMode) => {
    if (!isMatchMode) {
        // 不是心动模式
        const result = await request.get('/user/recommend');
        if (result.data.code === 200) {
            return result.data.data;
        } else {
            showFailToast('获取推荐用户失败！');
        }
    } else {
        // 是心动模式
        const result = await request.get('/user/match');
        if (result.data.code === 200) {
            return result.data.data;
        } else {
            showFailToast('获取推荐用户失败！');
        }
    }

}

// 获取全部子级标签
const getChildrenTagList = async () => {
    const res = await request.get('/tags/list/children');
    if (res.data && res.data?.code === 200) {
        // 证明获取数据成功
        return res.data.data;
    } else {
        // 获取数据失败
        return [];
    }
}

// 解散队伍接口
const dissolutionTeam = async (teamId: number) => {
    const res = await request.post('/team/dissolution', {
        teamId: teamId
    });
    return res.data?.code === 200 && res.data?.data === true;
}

// 退出队伍接口
const quitTeam = async (teamId: number) => {
    const res = await request.post('/team/quit', {
        teamId: teamId
    });
    return res.data?.code === 200 && res.data?.data === true;
}

export {
    doLogin, // 执行登录逻辑
    getCurrentUser, // 获取当前登录用户
    getRecommendUsers, // 获取推荐用户
    dissolutionTeam, // 解散队伍
    quitTeam, // 退出队伍
    getChildrenTagList // 获取全部子级标签
};